<template>
  <div class="modification">
    <div class="card text-left">
      <div class="card-body">
        <h4 class="card-title">修改个人头像</h4>
        <el-upload
          action
          :on-change="handleelchange"
          :auto-upload="false"
          class="upload-demo avatar"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-button @click="goback">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imageUrl: '',
    }
  },

  methods: {
    handleelchange(file, fileList) {
      console.log(file, fileList)
      let formdata = new FormData()
      fileList.map((item) => {
        //fileList本来就是数组，就不用转为真数组了
        formdata.append('avatar', item.raw) //将每一个文件图片都加进formdata
      })
      this.$http
        .post(
          '/sys/user/modifyAvatar/' + this.$store.getters.getUserInfo.id,
          formdata
        )
        .then((res) => {
          if (res.data.code == 200) {
            this.imageUrl = res.data.data.avatar
            this.$message.success(res.data.msg)
            this.$router.push('/userInfo')
          }
        })
    },
    goback() {
      this.$router.go(-1)
    },
  },
}
</script>

<style lang="less" scoped>
.modification {
  height: 100vh;
  background: url(https://cdn.syjun.vip/background.png) no-repeat 100% 100%;
  .card {
    background: transparent;
    border: none !important;
  }
}
.avatar {
  margin: 30px auto;
  border: 1px dashed #523838;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload {
  border: 1px dashed #523838;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
